package com.livableindex.controller;

import com.livableindex.entity.WeatherData;
import com.livableindex.service.WeatherDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class MainController {
    
    @Autowired
    private WeatherDataService weatherDataService;
    
    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("provinces", weatherDataService.getAllProvinces());
        return "index";
    }
    
    @GetMapping("/search")
    public String searchPage(Model model) {
        model.addAttribute("provinces", weatherDataService.getAllProvinces());
        return "search";
    }
    
    @GetMapping("/api/cities/{province}")
    @ResponseBody
    public List<String> getCities(@PathVariable String province) {
        return weatherDataService.getCitiesByProvince(province);
    }
    
    @GetMapping("/api/districts/{province}/{city}")
    @ResponseBody
    public List<String> getDistricts(@PathVariable String province, @PathVariable String city) {
        return weatherDataService.getDistrictsByProvinceAndCity(province, city);
    }
    
    @GetMapping("/dashboard")
    public String dashboard(@RequestParam String province,
                           @RequestParam String city,
                           @RequestParam String district,
                           Model model) {
        List<WeatherData> weatherData = weatherDataService.getWeatherDataByDistrict(province, city, district);
        Double livableIndex = weatherDataService.calculateLivableIndex(province, city, district);
        
        model.addAttribute("weatherData", weatherData);
        model.addAttribute("livableIndex", livableIndex);
        model.addAttribute("province", province);
        model.addAttribute("city", city);
        model.addAttribute("district", district);
        
        return "dashboard";
    }
    
    @GetMapping("/compare")
    public String comparePage(Model model) {
        model.addAttribute("provinces", weatherDataService.getAllProvinces());
        return "compare";
    }
}
